package TrafficSim;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;

import javax.vecmath.Vector2d;

public class TrafficGraph {

	private Vector<Intersection> intersections = new Vector<Intersection>();

	public void init() {
		// Init some Intersections
		Intersection I1 = new Intersection(1, 10, 10);
		Intersection I2 = new Intersection(2, 250, 10);
		Intersection I3 = new Intersection(3, 750, 10);
		Intersection I4 = new Intersection(4, 10, 200);
		Intersection I5 = new Intersection(5, 250, 200);
		Intersection I6 = new Intersection(6, 750, 200);
		Intersection I7 = new Intersection(7, 250, 350);
		Intersection I8 = new Intersection(8, 480, 350);
		Intersection I9 = new Intersection(9, 10, 500);
		Intersection I10 = new Intersection(10, 250, 500);
		Intersection I11 = new Intersection(11, 750, 500);
		
		

		// add Intersections to the graph
		this.addIntersection(I1);
		this.addIntersection(I2);
		this.addIntersection(I3);
		this.addIntersection(I4);
		this.addIntersection(I5);
		this.addIntersection(I6);
		this.addIntersection(I7);
		this.addIntersection(I8);
		this.addIntersection(I9);
		this.addIntersection(I10);
		this.addIntersection(I11);

		// make (OneWayStreet-)Links between Intersections
		I1.appendStreet(I4, new Street(70, "test1"));
		I2.appendStreet(I1, new Street(30, "test2"));
		I2.appendStreet(I3, new Street(70, "test3"));
		I3.appendStreet(I2, new Street(30, "test4"));
		I3.appendStreet(I6, new Street(40, "test5"));
		I4.appendStreet(I1, new Street(50, "test6"));
		I4.appendStreet(I5, new Street(30, "test7"));
		I4.appendStreet(I9, new Street(50, "test8"));
		I5.appendStreet(I2, new Street(50, "test9"));
		I5.appendStreet(I4, new Street(50, "test10"));
		I2.appendStreet(I5, new Street(50, "test11"));
		I1.appendStreet(I2, new Street(50, "test12"));
		I5.appendStreet(I6, new Street(50, "test13"));
		I5.appendStreet(I7, new Street(50, "test14"));
		I6.appendStreet(I3, new Street(80, "test15"));
		I6.appendStreet(I5, new Street(30, "test16"));
		I6.appendStreet(I8, new Street(50, "test17"));
		I6.appendStreet(I11, new Street(50, "test18"));
		I7.appendStreet(I5, new Street(80, "test19"));
		I7.appendStreet(I8, new Street(30, "test20"));
		I7.appendStreet(I10, new Street(30, "test21"));
		I8.appendStreet(I7, new Street(30, "test22"));
		I8.appendStreet(I6, new Street(50, "test23"));
		I9.appendStreet(I4, new Street(50, "test24"));
		I10.appendStreet(I7, new Street(30, "test25"));
		I10.appendStreet(I11, new Street(50, "test26"));
		I11.appendStreet(I6, new Street(70, "test27"));
		I11.appendStreet(I10, new Street(50, "test28"));
		// I1.appendStreet(I11, new Street(1,50));
		// I11.appendStreet(I1, new Street(1,50));

		for (Intersection i : this.intersections) {
			i.sort();
			if(i.getIncomingStreets().size()>2){
			i.addTrafficLightController();
			}
		}
	}
	public void initMichi(){
		// Init some Intersections
		Intersection I2 = new Intersection(2, 250, 10);
		Intersection I4 = new Intersection(4, 10, 200);
		Intersection I5 = new Intersection(5, 250, 200);
		Intersection I6 = new Intersection(6, 750, 200);
		Intersection I7 = new Intersection(7, 250, 350);
		
		

		// add Intersections to the graph
		this.addIntersection(I2);
		this.addIntersection(I4);
		this.addIntersection(I5);
		this.addIntersection(I6);
		this.addIntersection(I7);

		// make (OneWayStreet-)Links between Intersections
		I4.appendStreet(I5, new Street(30, "test7"));
		I5.appendStreet(I2, new Street(50, "test9"));
		I5.appendStreet(I4, new Street(50, "test10"));
		I2.appendStreet(I5, new Street(50, "test11"));
		I5.appendStreet(I6, new Street(50, "test13"));
		I5.appendStreet(I7, new Street(50, "test14"));
		I6.appendStreet(I5, new Street(30, "test16"));
		I7.appendStreet(I5, new Street(80, "test19"));
		// I1.appendStreet(I11, new Street(1,50));
		// I11.appendStreet(I1, new Street(1,50));

		for (Intersection i : this.intersections) {
			i.sort();
			if(i.getIncomingStreets().size()>2){
			i.addTrafficLightController();
			}
		}
		
	}
	public void initAugsburg(){
		readIntersections();
		readStreets();
		addTrafficLights();
		double km = 0;
		for (Intersection i : this.intersections) {
			for (Intersection j: i.getNextIntersections()){
				Street s = i.getStreet(j);
				Vector2d start = new Vector2d(s.getStart().x, s.getStart().y);
				start.sub(new Vector2d(s.getEnd().x, s.getEnd().y));
				km+= start.length();
				if (j.getStreet(i) == null){
					s.isNotOneWay();
				}
			}
		}
		System.out.println("total: " + km);
	}
	public void readStreets() {
		try {
			BufferedReader reader = new BufferedReader(new FileReader(
					"./Streets.txt"));
			StringBuffer output = new StringBuffer();
			String line;
			int id = 0;
			while ((line = reader.readLine()) != null) {
				output.append(line + "\n");
				String [] streets = line.split(" ");
				Intersection temp = this.intersections.get(id);
				for (int i = 1; i < streets.length; i++){
					String[] street = streets[i].split(",");				
				Intersection next = this.intersections.get(Integer.parseInt(street[0])-1);
				int speed = Integer.parseInt(street[1]);
				temp.appendStreet(next, new Street(speed));
				}
				id++;
			}
			reader.close();
			
			

		} catch (IOException e) {
			
		}
	}
	public void addTrafficLights(){
		try {
			BufferedReader reader = new BufferedReader(new FileReader(
					"./Streets.txt"));
			StringBuffer output = new StringBuffer();
			String line;
			int id = 0;
			while ((line = reader.readLine()) != null) {
				output.append(line + "\n");
				String [] streets = line.split(" ");
				Intersection temp = this.intersections.get(id);
				temp.sort();
				if (streets[0].contains("t")){
					temp.addTrafficLightController();
				}
				id++;
			}
			reader.close();
			
			

		} catch (IOException e) {
			
		}
	}
	public void readIntersections() {
		try {
			BufferedReader reader = new BufferedReader(new FileReader(
					"./Intersections.txt"));
			StringBuffer output = new StringBuffer();
			String line;
			int id = 1;
			while ((line = reader.readLine()) != null) {
				output.append(line + "\n");
				String [] coords = line.split(",");
				int x = Integer.parseInt(coords[0]);
				int y = Integer.parseInt(coords[1]);
				x = (int)(0.78 * x);
				y = (int)(0.78 * y);
				Intersection I1 = new Intersection(id, x, y);
				this.addIntersection(I1);
//				I1.appendStreet(I1, new Street(50));
				id++;
			}
			reader.close();
			
			

		} catch (IOException e) {
			
		}
	}

	public Vector<Intersection> getGraph() {
		return this.intersections;
	}

	public void addIntersection(Intersection i) {
		this.intersections.add(i);
	}

}
